package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.config.VodConstantProperties;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.util.UploadFileUtil;
import com.atguigu.tingshu.vo.album.TrackMediaInfoVo;
import com.qcloud.vod.VodUploadClient;
import com.qcloud.vod.model.VodUploadRequest;
import com.qcloud.vod.model.VodUploadResponse;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.vod.v20180717.VodClient;
import com.tencentcloudapi.vod.v20180717.models.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;


@Service
@Slf4j
public class VodServiceImpl implements VodService {

    @Autowired
    private VodConstantProperties vodConstantProperties;

    @Autowired
    private VodUploadClient vodUploadClient;

    @Autowired
    private Credential cred;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, String> uploadTrack(MultipartFile file) {
        try {
            if (file == null) {
                log.error("上传文件为空！");
                throw new GuiguException(500, "上传文件为空！");
            }
            //上传的本地
            String tempPath = UploadFileUtil.uploadTempPath(vodConstantProperties.getTempPath(), file);

            //上传到腾讯云点播
            //设置媒体上传的路径
            VodUploadRequest request = new VodUploadRequest();
            request.setMediaFilePath(tempPath);
            //上传音频
            VodUploadResponse response = vodUploadClient.upload(vodConstantProperties.getRegion(), request);
            //不需要审核 因为审核时保存业务做的事情
            //获取请求路径和唯一的id
            if (response != null) {
                String fileId = response.getFileId();
                String mediaUrl = response.getMediaUrl();
                return Map.of("mediaFileId", fileId, "mediaUrl", mediaUrl);
            }
            return null;
        } catch (Exception e) {
            log.error("上传失败！");
            throw new RuntimeException(e);
        }
    }

    @Override
    public TrackMediaInfoVo getTrackMediaInfo(String mediaFileId) {
        try {
            //1.实例化要请求产品的client对象,clientProfile是可选的
            VodClient client = new VodClient(cred, vodConstantProperties.getRegion());
            //2.实例化一个请求对象,每个接口都会对应一个request对象
            DescribeMediaInfosRequest req = new DescribeMediaInfosRequest();
            String[] fileIds1 = {mediaFileId};
            req.setFileIds(fileIds1);
            //3.返回的resp是一个DescribeMediaInfosResponse的实例，与请求对象对应
            DescribeMediaInfosResponse resp = client.DescribeMediaInfos(req);
            if (resp != null) {
                MediaInfo mediaInfo = resp.getMediaInfoSet()[0];
                MediaBasicInfo basicInfo = mediaInfo.getBasicInfo();
                TrackMediaInfoVo trackMediaInfoVo = new TrackMediaInfoVo();
                MediaMetaData metaData = mediaInfo.getMetaData();
                trackMediaInfoVo.setType(basicInfo.getType());
                trackMediaInfoVo.setSize(metaData.getSize());
                trackMediaInfoVo.setDuration(metaData.getDuration());
                return trackMediaInfoVo;
            }

        } catch (TencentCloudSDKException e) {
            throw new RuntimeException(e);
        }
        return null;
    }


    /**
     * 删除音视频文件
     *
     * @param mediaFileId
     */
    @Override
    public void deleteMedia(String mediaFileId) {
        try {
            //1.实例化一个认证对象
            Credential cred = new Credential(vodConstantProperties.getSecretId(), vodConstantProperties.getSecretKey());
            //2.实例化要请求产品的client对象,clientProfile是可选的
            VodClient client = new VodClient(cred, vodConstantProperties.getRegion());
            //3.实例化一个请求对象,每个接口都会对应一个request对象
            DeleteMediaRequest req = new DeleteMediaRequest();
            req.setFileId(mediaFileId);
            //4.返回的resp是一个DeleteMediaResponse的实例，与请求对象对应
            client.DeleteMedia(req);
        } catch (TencentCloudSDKException e) {
            log.error("[专辑服务]删除点播平台文件异常：{}", e);
        }
    }
}
